﻿using Common.Client.Lib.Model;
using CommunityToolkit.Mvvm.ComponentModel;

namespace Syncfusion.Lib.Data
{
    /// <summary>
    /// 用于Region加载SfDockingManager下Content的一些属性设置
    /// </summary>
    public class SfDockingManagerContentProperty : ObservableObject
    {

        /// <summary>
        /// SfDockingManager内容属性
        /// </summary>
        public SfDockingManagerContentProperty()
        {

        }

        #region 功能

        /// <summary>
        /// 功能
        /// </summary>
        private FunctionModel? _function;

        /// <summary>
        /// 功能
        /// </summary>
        public FunctionModel? Function
        {
            get => _function;
            set => SetProperty(ref _function, value);
        }

        #endregion 功能 


        #region 附加数据
        /// <summary>
        /// 附加数据，例如存储对应的功能菜单
        /// </summary>
        public Dictionary<string, object?> AttachData { get; set; } = new Dictionary<string, object?>();

        /// <summary>
        /// 获取附加数据
        /// </summary>
        /// <param name="key">附加数据唯一键，大小写敏感</param>
        /// <param name="defaultValue">附加数据获取失败的默认值</param>
        /// <returns></returns>
        public object? GetAttachData(string key, object? defaultValue = null)
        {
            object? res = defaultValue;
            if (AttachData.TryGetValue(key, out var value))
            {
                res = value;
            }
            return res;
        }

        /// <summary>
        /// 获取附加数据
        /// </summary>
        /// <param name="key">附加数据唯一键，大小写敏感</param>
        /// <param name="value">附加数据设置的值</param>
        /// <returns></returns>
        public bool SetAttachData(string key, object? value = null)
        {
            bool res = false;
            if (AttachData.ContainsKey(key))
            {
                AttachData[key] = value;
            }
            else
            {
                res = AttachData.TryAdd(key, value);
            }
            return res;
        }

        /// <summary>
        /// 设置附加数据
        /// </summary>
        /// <param name="key">附加数据唯一键，大小写敏感</param>
        /// <param name="value">附加数据设置的值</param>
        /// <returns></returns>
        public bool SetAttachDataT<T>(string key, T? value = default)
        {
            return SetAttachData(key, value);
        }

        /// <summary>
        /// 获取附加数据
        /// </summary>
        /// <param name="key">附加数据唯一键，大小写敏感</param>
        /// <param name="defaultValue">附加数据获取失败的默认值</param>
        /// <returns></returns>
        public T? GetAttachData<T>(string key, T? defaultValue = default)
        {
            T? res = defaultValue;
            object? objectRes = GetAttachData(key);
            try
            {
                object? objectResChangeType = Convert.ChangeType(objectRes, typeof(T?));
                res = (T?)objectResChangeType;
            }
            catch
            {
                // ignored
            }

            return res;
        }

        #endregion 附加数据

    }
}
